﻿package com.PlantVsZombies.Bullet;

import java.awt.Graphics;
import java.awt.Image;
import java.io.Serializable;

import com.PlantVsZombies.common.Life;
import com.PlantVsZombies.constant.Constant;
import com.PlantVsZombies.util.ImageUtil;

/*
 * 子弹类
 * 
 * 
 */
public class Bullet extends Life implements Constant, Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	int x; // 子弹坐标
	int y;
	Image image; // 子弹图片
	int direct = 1;
	int move0 = 1;
	int move1 = 1;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Bullet(int x, int y) {
		this.x = x;
		this.y = y;
		this.image = ImageUtil.bullet1;
	}

	public Bullet(int x, int y, Image image, int direct) {
		this.x = x;
		this.y = y;
		this.image = image;
		this.direct = direct;
	}

	public void drawMe(Graphics g) {
		g.drawImage(image, x + PLANT_WIDTH / 2 + 20, y + 3, null);
	}

	public void drawMe(Graphics g, int w, int h) {
		g.drawImage(image, x + PLANT_WIDTH / 2 + 20, y + 3, w, h, null);
	}

	public void shoot() {
		switch(direct){
		case 0:
			this.x--;
			break;
		case 1:
			this.x++;
			break;
		case 2:
			this.x--;
			this.y++;
			break;
		case 3:
			this.x -= 1;
			this.y -= move0;
			if (this.y < 20) {
				move0 = -move0;
			}
			if (this.y > 450)
				move0 = -move0;
			break;
		case 4:
			this.x--;
			this.y+=move1;
			if (this.y < 20) {
				move1 = -move1;
			}
			if (this.y > 450)
				move1 = -move1;
			break;
		case 5:
			this.x--;
			break;	
		}
	}
	@Override
	public int doRun() {
		shoot();
		try {
			Thread.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (this.x > 1100 || this.x<-100 || this.y<-100 || this.y>700) {
			setLive(false);
			return 0;
		}
		return 1;
	}
}
